import tkinter as tk
from math import cos, sin, pi
from datetime import datetime


class ClockApp(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("表盘时钟")
        self.geometry("400x450")
        self.configure(bg='light green')

        self.motto_label = tk.Label(self, text="时不待我，只争朝夕！", font=('楷体', 16, 'bold'), bg='light green')
        self.motto_label.pack()

        self.canvas = tk.Canvas(self, width=400, height=360, bg='light green', highlightthickness=0)
        self.canvas.pack()

        self.time_label = tk.Label(self, font=('Helvetica', 16), bg='light green')
        self.time_label.pack(pady=10)

        self.date_label = tk.Label(self, font=('Helvetica', 12), bg='light green')
        self.date_label.pack()

        self.hour_hand = None  # 保存时针图像对象
        self.minute_hand = None  # 保存分针图像对象
        self.second_hand = None  # 保存秒针图像对象

        self.draw_clock()
        self.update_clock()

    def draw_clock(self):
        center_x = 200
        center_y = 200
        radius = 150

        # 绘制表盘
        self.canvas.create_oval(center_x - radius, center_y - radius, center_x + radius, center_y + radius,
                                width=2, outline='blue')

        # 绘制刻度线和小时标识
        for i in range(12):
            angle = (i / 12) * 2 * pi - 1 / 3 * pi  # 这里添加减去1/3 pi目的是为了让表盘顶部是12点，底部是6点
            x1 = center_x + cos(angle) * (radius - 10)
            y1 = center_y + sin(angle) * (radius - 10)
            x2 = center_x + cos(angle) * radius
            y2 = center_y + sin(angle) * radius
            self.canvas.create_line(x1, y1, x2, y2, width=3)
            hour_x = center_x + cos(angle) * (radius - 25)
            hour_y = center_y + sin(angle) * (radius - 25)
            self.canvas.create_text(hour_x, hour_y, text=str(i + 1), font=('Helvetica', 12), fill='black')

    def update_clock(self):
        current_time = datetime.now().time()
        current_date = datetime.now().date()

        # 更新时间标签
        time_str = current_time.strftime('%H:%M:%S')
        self.time_label.config(text=time_str)

        # 更新日期标签
        date_str = current_date.strftime('%Y-%m-%d')
        self.date_label.config(text=date_str)

        # 删除上一次的时针和分针图像
        if self.hour_hand:
            self.canvas.delete(self.hour_hand)
        if self.minute_hand:
            self.canvas.delete(self.minute_hand)

        # 更新时针
        hour = current_time.hour
        minute = current_time.minute
        second = current_time.second

        hour_angle = ((hour % 12) + minute / 60 + second / 3600) * (2 * pi / 12) - pi / 2
        hour_length = hour_x = 200 + cos(hour_angle) * 60
        hour_y = 200 + sin(hour_angle) * 60
        self.hour_hand = self.canvas.create_line(200, 200, hour_x, hour_y, width=4, fill='brown')

        # 更新分针
        minute_angle = ((minute + second / 60) / 60) * (2 * pi) - pi / 2
        minute_length = 90
        minute_x = 200 + cos(minute_angle) * minute_length
        minute_y = 200 + sin(minute_angle) * minute_length
        self.minute_hand = self.canvas.create_line(200, 200, minute_x, minute_y, width=3, fill='green')

        # 更新秒针
        second_angle = (second / 60.0) * (2 * pi) - pi / 2
        second_length = 120
        second_x = 200 + cos(second_angle) * second_length
        second_y = 200 + sin(second_angle) * second_length
        if self.second_hand:
            self.canvas.delete(self.second_hand)
        self.second_hand = self.canvas.create_line(200, 200, second_x, second_y, width=2, fill='red')

        self.after(1000, self.update_clock)


if __name__ == "__main__":
    app = ClockApp()
    app.mainloop()